package com.iszhouhua.blog.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iszhouhua.blog.common.util.Result;
import com.iszhouhua.blog.model.Category;
import com.iszhouhua.blog.model.Video;
import com.iszhouhua.blog.service.CategoryService;
import com.iszhouhua.blog.service.VedioService;
import com.iszhouhua.blog.service.impl.VedioServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;

/**
 * 视频管理
 *
 * @author xiaojin
 * @since 2020-10-10
 */
@RestController
@RequestMapping("api/video")
public class ApiVedioController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private VedioService vedioService;

  /**
   * 分页查询
   * @param page
   * @return
   */
    @GetMapping("list")
    public Result list(Page<Video> page, Video video) {
      String title = video.getTitle();
//      picture.setDescription(null);
      Long categoryId = video.getCategoryId();

      QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
      if (StringUtils.isNotBlank(title)){
        queryWrapper.like("title",title);
      }
      if (categoryId != null){
        queryWrapper.eq("category_id",categoryId);
      }
      IPage<Video> pictureIPage = vedioService.page(page,queryWrapper);
      queryWrapper.orderByDesc("update_time");
      for (Video video1 : pictureIPage.getRecords()){
        Category category = categoryService.getById(video1.getCategoryId());
        video1.setCategory(category);
      }
      return Result.success("查询成功", pictureIPage);
    }

    /**
     * 添加视频
     *
     * @param video
     * @return
     * @throws Exception
     */
    @PostMapping
    public Result save(@RequestBody Video video) throws Exception {
      video.setCreateTime(new Date());
      video.setUpdateTime(new Date());
      boolean b = vedioService.save(video);
      if (!b){
        return Result.fail("新增失败", null);
      }
        return Result.success("新增成功", null);
    }

  /**
   * 修改视频
   * @param video
   * @param session
   * @return
   */
    @PutMapping
   public Result update(@RequestBody Video video, HttpSession session) {
      Video oldvideo = vedioService.getById(video.getId());
      UpdateWrapper<Video> wrapper = new UpdateWrapper<>();
      BeanUtils.copyProperties(video, oldvideo);
      oldvideo.setUpdateTime(new Date());
      boolean b = vedioService.update(oldvideo, wrapper.eq("id", video.getId()));
      if (b){
        return Result.success("修改成功", oldvideo);
      }
      return Result.fail("修改失败",null);

    }

  /**
   * 根据id获取视频
   * @param id
   * @return
   */
    @GetMapping
    public Result info(Long id) {
      Video video = vedioService.getById(id);
      Category category = categoryService.getById(video.getCategoryId());
      video.setCategory(category);
      return Result.success("获取成功", video);
    }

  /**
   * 删除
   * @param id
   * @return
   */
    @DeleteMapping
    public Result delete(Long id){
      try {
        Video video = vedioService.getById(id);
        //去掉前22个字符
        String path = video.getVideo().substring(22);
        System.out.println(path);
        File file = new File("C:\\Users\\xiaojin\\Desktop\\blog-master\\src\\main\\resources\\static\\"+path);
        if (file.exists()){
          file.delete();
          vedioService.removeById(id);
          return Result.success("删除成功");
        }else {
          return Result.success("删除失败");
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return Result.success("删除失败或者改图片不存在");
    }


}
